package com.bk.cyxf.ft;

import com.bk.cyxf.log.LogManager;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.object.IObject;
import jt.classic.system.object.ObjectLCManager;
import jt.classic.system.user.IUser;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.StringTool;

import java.sql.Connection;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 案件过滤action
 *
 * @author lvt
 *
 */
public class FtAction extends SimpleAction {
    /**
     * 根据获取的id值将案件置为已过滤通过状态
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward ft(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        CommonBean4HSQ req = new CommonBean4HSQ(request);
        Map param = req.getResource();

        String ftstatus = StringTool.checkString(param.get("ftstatus"));
        String[] ids = StringTool.checkString(param.get("ids")).split(",");
        IUser user = ISystem.getSessionUser(request);

        String ftlabel = ftstatus.equals("1") ? "yes" : "no";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            if ((ids != null) && (ids.length > 0)) {
                LogManager lm = new LogManager(conn);
                BaseworkUtil util = new BaseworkUtil();
                ObjectLCManager olcm = new ObjectLCManager(conn);

                for (int i = 0; i < ids.length; i++) {
                    if ((ids[i] != null) && !ids[i].equals("")) {
                        IObject obj = olcm.getObjectById(ids[i]);

                        if (obj.ikey().equals("APP_LXDJ")) {
                            List list = (List) obj.icontent(obj.iobject()
                                                               .itable("APP_LX"));

                            String lxid = (String) ((Map) list.get(0)).get(
                                    "LXID");

                            SimpleBean4Table sbt = new SimpleBean4TableImpl(obj.iobject()
                                                                               .itable("APP_LX"));

                            sbt.getResource().put("lxid", lxid);
                            sbt.getResource().put("FTSTATUS", ftstatus);

                            util.manualUpdate1(sbt, conn);

                            lm.log(user, lxid, "0", "lxft" + ftlabel, "");
                        } else if (obj.ikey().equals("APP_LFDJ")) {
                            List list = (List) obj.icontent(obj.iobject()
                                                               .itable("APP_LF"));

                            String lfid = (String) ((Map) list.get(0)).get(
                                    "LFID");

                            SimpleBean4Table sbt = new SimpleBean4TableImpl(obj.iobject()
                                                                               .itable("APP_LF"));

                            sbt.getResource().put("lfid", lfid);
                            sbt.getResource().put("FTSTATUS", ftstatus);

                            util.manualUpdate1(sbt, conn);

                            lm.log(user, lfid, "1", "lfft" + ftlabel, "");
                        }
                    }
                }

                conn.commit();
            }

            request.setAttribute("result", "{success:true}");
        } catch (Exception ex) {
            request.setAttribute("result", "{success:false}");

            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }
}
